//
//  ShowPicture.m
//  BeautyPicture
//
//  Created by 沈振邦 on 16/2/19.
//  Copyright © 2016年 szb. All rights reserved.
//

#import "ShowPicture.h"
#import "BPImageTool.h"

@implementation ShowPicture



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //1.毛玻璃 处理
        UIVisualEffectView *visualEffect = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
        
        visualEffect.frame = frame;
        
        visualEffect.alpha = 0.9;
        
        [self addSubview:visualEffect];
        
        _dataArry = [NSMutableArray array];
        
        //2.设置浏览图片视图
        CGFloat cax = 50.0f;
        _carousel = [[iCarousel alloc] initWithFrame:CGRectMake(cax - 25.0f, 50, BPScreenW - 50, BPScreenH - 100)];
        _carousel.type = iCarouselTypeRotary;
        _carousel.pagingEnabled = YES;
        _carousel.delegate = self;
        _carousel.dataSource = self;
        [self addSubview:_carousel];
        
        //3.设置按钮
        _startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat sbtnW = 100;
        _startBtn.frame = CGRectMake((BPScreenW - sbtnW) / 2.0, BPScreenH - 70, sbtnW, 30);
        _startBtn.layer.cornerRadius = 15.0f;
        [_startBtn setTitle:@"开始制作" forState:UIControlStateNormal];
        [_startBtn setTintColor:[UIColor blackColor]];
        _startBtn.backgroundColor = [UIColor whiteColor];
        [_startBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self addSubview:_startBtn];
        
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _closeBtn.frame = CGRectMake(10, 30, 50, 50);
        _closeBtn.contentMode = UIViewContentModeScaleAspectFit;
        [_closeBtn setImage:[UIImage imageNamed:@"deleteBtn"] forState:UIControlStateNormal];
        [self addSubview:_closeBtn];
        
        [_closeBtn addTarget:self action:@selector(closeShowView) forControlEvents:UIControlEventTouchUpInside];
        
        //4.设置提示文字 (显示, 0 /13的 "0")
        _pageLabel = [[UILabel alloc] initWithFrame:CGRectMake(BPScreenW - 60, 30, 30, 30)];
        _pageLabel.font = [UIFont boldSystemFontOfSize:18.0f];
        [self addSubview:_pageLabel];
        
        _pageTotal = [[UILabel alloc] initWithFrame:CGRectMake(BPScreenW - 40, 30, 30, 30)];
        _pageTotal.font = [UIFont systemFontOfSize:12.0f];
        [self addSubview:_pageTotal];
        
        //出现的效果
        [self fadeViewIn];
        //全屏显示
        [[UIApplication sharedApplication].keyWindow addSubview:self];

    }
    return self;
}

- (void)setUppageNumAndShowPic:(NSMutableArray *)picUrl withCurrentPage:(NSInteger)index
{
    _dataArry = picUrl;
    _pageTotal.text = [NSString stringWithFormat:@"/%lu",(unsigned long)_dataArry.count];
    _pageLabel.text = [NSString stringWithFormat:@"%ld",(long)index];
    _pageLabel.text = @"1";
    _carousel.currentItemIndex = index;
    [_carousel reloadData];
}

- (CATransform3D)carousel:(__unused iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
    //implement 'flip3D' style carousel
    transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);
    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * _carousel.itemWidth);
}

- (NSInteger)numberOfItemsInCarousel:(__unused iCarousel *)carousel
{
    return (NSInteger)[_dataArry count];
}

- (UIView *)carousel:(__unused iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
//    UILabel *label = nil;
    
    //create new view if no view is available for recycling
    if (view == nil)
    {
        view = [[UIImageView alloc] initWithFrame:_carousel.bounds];
        ((UIImageView *)view).image = [UIImage imageNamed:@"page.png"];
        view.contentMode = UIViewContentModeScaleAspectFit;
        view.backgroundColor = [UIColor whiteColor];
        view.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
        view.layer.shadowOffset = CGSizeMake(4,4);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
        view.layer.shadowOpacity = 0.8;//阴影透明度，默认0
        view.layer.shadowRadius = 4;
        //添加网络图片
        [BPImageTool setUpUIImageView:((UIImageView *)view) addNsstringUrl:_dataArry[index]];
        
        // 把view的背景颜色变成透明的
        view.backgroundColor = [UIColor clearColor];
    }
    
    return view;
}

- (void)carousel:(__unused iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
//    NSNumber *item = (self.items)[(NSUInteger)index];
//    NSLog(@"Tapped view number: %@", item);
}

- (void)carouselCurrentItemIndexDidChange:(__unused iCarousel *)carousel
{
    //NSLog(@"Index: %@", @(self.carousel.currentItemIndex));
    _pageLabel.text = [NSString stringWithFormat:@"%ld",(long)_carousel.currentItemIndex + 1];
}


//关闭的效果
- (void)closeShowView
{
    [UIView animateWithDuration:0.335 animations:^{
        self.alpha = 0.0f;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)fadeViewIn
{
    self.alpha = 0.0f;
    
    [UIView animateWithDuration:0.335 animations:^{
        self.alpha = 1.0f;
    } completion:^(BOOL finished) {
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
